package com.whacos.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whacos.common.utils.StringUtils;
import com.whacos.modules.system.dao.DictDao;
import com.whacos.modules.system.entity.Dict;
import com.whacos.modules.system.entity.SysUser;
import com.whacos.modules.system.service.DictService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author xiongdun
 * @description SysDictServiceImpl
 * @since 2018/11/17 17:11
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictDao, Dict> implements DictService {


    @Cacheable(value = "sysCache")
    @Override
    public List<Dict> listType() {
        return baseMapper.listType();
    }

    @Override
    public String getName(String type, String value) {
        return this.baseMapper.selectList(Wrappers.lambdaQuery(new Dict())
                .eq(Dict::getDictType, type)
                .eq(Dict::getValue, value)).get(0).getKey();
    }

    @Cacheable(value = "sysCache")
    @Override
    public List<Dict> getHobbyList(SysUser sysUser) {
        List<Dict> hobbyList = this.listByType("hobby");

        if (StringUtils.isNotEmpty(sysUser.getHobby())) {
            String[] userHobbies = sysUser.getHobby().split(";");
            for (String userHobby : userHobbies) {
                for (Dict hobby : hobbyList) {
                    if (!Objects.equals(userHobby, hobby.getId().toString())) {
                        continue;
                    }
                    hobby.setRemark("true");
                    break;
                }
            }
        }

        return hobbyList;
    }

    @Cacheable(value = "sysCache")
    @Override
    public List<Dict> getSexList() {
        return this.listByType("sex");
    }

    @Override
    public List<Dict> listByType(String type) {
        return this.baseMapper.selectList(Wrappers.lambdaQuery(new Dict())
                .eq(Dict::getDictType, type));
    }

}
